/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 // 第一种,使用后序遍历(DFS)实现
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
    }
}



// 第二种,使用层序遍历(BFS)实现,借助队列
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        int res = 0;
        while(!qu.isEmpty()){
            int size = qu.size();
            for(int i = 0; i < size; i++){
                TreeNode node = qu.poll();
                if(node.left != null) qu.offer(node.left);
                if(node.right != null) qu.offer(node.right);
            }
            res++;
        }
        return res;
    }
}